package com.数据结构.堆;

import com.数据结构.线性表.ArrayList;
import org.checkerframework.checker.units.qual.A;

public class MaxHeap {
    private ArrayList<Integer>data;

    public MaxHeap(int capacity){
        this.data = new ArrayList(capacity);
    }

    public boolean isEmpty(){
        return data.isEmpty();
    }

    public int size(){
        return data.size();
    }

    private int parent(int index){
        if(index==0){
            throw new RuntimeException("无根节点");
        }
        return (index-1)/2;
    }

    private int left(int index){
        return 2*index+1;
    }
    private int right(int index){
        return 2*index+2;
    }

    public void insert(int ele){
        data.add(ele);
        shitUp(data.get(0));
    }

    private void shitUp(Integer index) {

        while (index>0 && data.get(index)>data.get(parent(index))){
            swap(index,parent(index));
            index = parent(index);
        }
    }
    public void swap(int a,int b){
        int tem = data.get(a);
        data.set(a,data.get(b));
        data.set(b,tem);
    }


}
